package com.zk.mall.servlet;

import com.zk.mall.entity.Order;
import com.zk.mall.service.OrderService;
import com.zk.mall.service.impl.OrderServiceImpl;
import com.zk.mall.util.JSONServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/back/getallOrderServlet")
public class getallOrderServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = new OrderServiceImpl();
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int pageIndex = Integer.parseInt(req.getParameter("pageNum"));
        int count = orderService.getOrderCount();
        if (pageIndex < 1) {
            pageIndex = 1;
        }
        int totalpages = count % pageSize == 0 ? count / pageSize : ((count / pageSize) + 1);
        if (pageIndex > totalpages) {
            pageIndex = totalpages;
        }
        List<Order> orderList = orderService.getallOrder(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pagesize", pageSize);
        map.put("pageindex", pageIndex);
        map.put("orderList", orderList);
        map.put("totalpages", totalpages);
        JSONServlet.getJson(req, resp, map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
